import {createRouter, createWebHistory, createWebHashHistory} from "vue-router";
import {useUserStore} from "@/stores";

export const routes = [

    {
        path: "/",
        name: "index",
        components: {
            default: () => import("../views/home/index.vue"),
        },
    },
    // {
    //     path: "/dashboard",
    //     name: "dashboard",
    //     components: {
    //         default: () => import("../layout/index.vue"),
    //     },
    // },


    {
        path: '/layout',
        name: 'layout',
        meta: {title: '列表展示'},
        component: () =>
            import( /* webpackChunkName: "home" */ '../layout/index.vue'),
        children: [
            {
                // 控制室
                path: '/layout/dashboard',
                name: 'dashboard',
                component: () =>
                    import( /* webpackChunkName: "popup" */ '../views/dashboard/index.vue'),
                meta: {
                    f: true
                }
            },
            {
                // 我的订单
                path: '/layout/myorder',
                name: 'myorder',
                component: () =>
                    import( /* webpackChunkName: "popup" */ '../views/myOrder/index.vue'),
                meta: {
                    f: true
                }
            },

            {
                // 上币申请
                path: '/layout/application',
                name: 'application',
                component: () =>
                    import( /* webpackChunkName: "popup" */ '../views/currencyApplication/index.vue'),
                meta: {
                    f: true
                }
            },
            {
                // 上币记录
                path: '/layout/record',
                name: 'record',
                component: () =>
                    import( /* webpackChunkName: "popup" */ '../views/record/index.vue'),
                meta: {
                    f: true
                }
            },
            {
                // 充币列表
                path: '/layout/rechargecurrency',
                name: 'rechargecurrency',
                component: () =>
                    import( /* webpackChunkName: "popup" */ '../views/rechargeCurrency/index.vue'),
                meta: {
                    f: true
                }
            },
            {
                // 提币列表
                path: '/layout/withdrawalcurrency',
                name: 'withdrawalcurrency',
                component: () =>
                    import( /* webpackChunkName: "popup" */ '../views/withdrawalCurrency/index.vue'),
                meta: {
                    f: true
                }
            },

            {
                // 交易回调
                path: '/layout/transactioncallback',
                name: 'transactionCallback',
                component: () =>
                    import( /* webpackChunkName: "popup" */ '../views/transactionCallback/index.vue'),
                meta: {
                    f: true
                }
            },
            {
                // 币种管理
                path: '/layout/currencymanagement',
                name: 'currencyManagement',
                component: () =>
                    import( /* webpackChunkName: "popup" */ '../views/currencyManagement/index.vue'),
                meta: {
                    f: true
                }
            },
            {
                // 资产管理
                path: '/layout/assetmanagement',
                name: 'assetManagement',
                component: () =>
                    import( /* webpackChunkName: "popup" */ '../views/assetManagement/index.vue'),
                meta: {
                    f: true
                }
            },
            {
                // 生成地址
                path: '/layout/generateaddress',
                name: 'generateAddress',
                component: () =>
                    import( /* webpackChunkName: "popup" */ '../views/generateAddress/index.vue'),
                meta: {
                    f: true
                }
            },
            {
                // 币种管理
                path: '/layout/currencyManagement',
                name: 'currencyManagement',
                component: () =>
                    import( /* webpackChunkName: "popup" */ '../views/currencyManagement/index.vue'),
                meta: {
                    f: true
                }
            },
            {
                // 批量生成钱包
                path: '/tools/batchwallet',
                name: 'batchWallet',
                component: () =>
                    import( /* webpackChunkName: "popup" */ '../views/web3Tools/batchWallet/index.vue'),
                meta: {
                    showTitle: true,
                    title: 'title.BatchWallet',
                },
            },
            {
                // 生成靓号钱包
                path: '/tools/beautifulwallet',
                name: 'beautifulWallet',
                component: () =>
                    import( /* webpackChunkName: "popup" */ '../views/web3Tools/beautifulWallet/index5.vue'),
                meta: {
                    showTitle: true,
                    title: 'title.BeautifulWallet',
                },
            },


        ]

    },
    {
        // 登录页
        path: "/login",
        name: "login",
        components: {
            default: () => import("../views/login/index.vue"),
        },
    },
    {
        // 注册页
        path: "/register",
        name: "register",
        components: {
            default: () => import("../views/reg/index.vue"),
        },
    },
    {
        // 忘记密码
        path: "/forget",
        name: "forget",
        components: {
            default: () => import("../views/forgetPW/index.vue"),
        },
    },
    {
        // 产品页
        path: "/product",
        name: "product",
        components: {
            default: () => import("../views/product/index.vue"),
        },
    },
    {
        // 开发者中心页
        path: "/developerCenter",
        name: "developerCenter",
        component: () =>
            import( /* webpackChunkName: "popup" */ '@/views/developerCenter/index4.vue'),
    },


    {
        // 测试页
        path: "/test",
        name: "test",
        components: {
            default: () => import("../views/test/index.vue"),
        },
    },

    //
    // {
    //     path: "/",
    //     redirect: "/index",
    //     meta: {},
    // },
];
const router = createRouter({
    // history: createWebHashHistory(),
    history: createWebHistory(),
    routes,
});

// 路由守卫
router.beforeEach((to, _, next) => {
    const noToken = ['/login', '/register', '/product', '/developerCenter', '/', '/forget'];
    if (noToken.includes(to.path)) {
        next();
    } else {
        const userStore = useUserStore();
        if (userStore.getToken) {
            next();
        } else {
            next("/login");
        }
    }
});

export default router;
